package com.chaodev.base.utils;

import com.alibaba.fastjson.JSONObject;
import com.chaodev.base.dto.MessageParam;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.core.env.Environment;


/**
 * 微信相关接口api
 *
 * @author zhao zu yun
 * @version 1.0
 * @since 2023/3/6 10:08
 */
public class WeixinApis {

    static Environment environment = SpringContextUtils.applicationContext.getEnvironment();

    private static final String APP_ID = environment.getProperty("weixin.gongzhonghao.appid");
    private static final String APP_SECRET = environment.getProperty("weixin.gongzhonghao.appsecret");





    public static JSONObject getAccessToken(String appId, String appSecret) {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret);
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;
        } catch (Exception error) {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }



    public static JSONObject getAccessTokenByCode(String code) {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APP_ID+"&secret="+APP_SECRET+"&code="+code+"&grant_type=authorization_code");
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;
        } catch (Exception error) {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }


    public static JSONObject sendMessage(MessageParam messageParam, String accessToken) {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" +accessToken);
            httpPost.setHeader("content-type", "application/json");
            StringEntity reqentity = new StringEntity(JSONObject.toJSONString(messageParam), "utf-8");
            httpPost.setEntity(reqentity);
            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;
        } catch (Exception error) {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }

    /**
     * 拉取用户信息
     * @return
     */
    public static JSONObject getWxUserInfo(String accessToken,String openid) {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openid+"&lang=zh_CN");
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;
        } catch (Exception error) {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }

//    public static JSONObject token(String appid,String secret)
//    {
//        JSONObject jsonObject = new JSONObject();
//        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//        CloseableHttpResponse httpResponse = null;
//        try {
//            HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret);
//            httpResponse = httpClient.execute(httpGet);
//            HttpEntity httpEntity = httpResponse.getEntity();
//            String res = EntityUtils.toString(httpEntity, "UTF-8");
//            jsonObject = JSONObject.parseObject(res);
//            return jsonObject;
//        } catch (Exception error) {
//            jsonObject.put("errcode", 405);
//            jsonObject.put("errmsg", error.getMessage());
//            return jsonObject;
//        }
//    }


    public static JSONObject getStableAccessToken(String appid,String secret){
        JSONObject jsonObject = new JSONObject();

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/stable_token");
            httpPost.setHeader("content-type", "application/json");

            String body="{\n" +
                    "    \"grant_type\": \"client_credential\",\n" +
                    "    \"appid\": \""+appid+"\",\n" +
                    "    \"secret\": \""+secret+"\"\n" +
                    "}";
            StringEntity reqentity = new StringEntity(body, "utf-8");
            httpPost.setEntity(reqentity);

            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;
        } catch (Exception error) {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }


    public static  JSONObject getPhoneNumber(String access_token,String code){
        JSONObject jsonObject = new JSONObject();

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+access_token);
            httpPost.setHeader("content-type", "application/json");

            String body="{\n" +
                    "\"code\": \""+code+"\"\n" +
                    "}";
            StringEntity reqentity = new StringEntity(body, "utf-8");
            httpPost.setEntity(reqentity);

            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;




        }
        catch (Exception error)
        {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }


    public static  JSONObject getPluginOpenPId(String access_token,String code){
        JSONObject jsonObject = new JSONObject();

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getpluginopenpid?access_token=" + access_token);
            httpPost.setHeader("content-type", "application/json");

            String body="{\n" +
                    "\"code\": \""+code+"\"\n" +
                    "}";
            StringEntity reqentity = new StringEntity(body, "utf-8");
            httpPost.setEntity(reqentity);

            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;


        }
        catch (Exception error)
        {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }

    public static  JSONObject code2Session(String appid,String secret,String code)
    {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code");
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;
        } catch (Exception error) {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }



    /**
     * 刷新微信用户AccessToken
     * @return
     */
    public static JSONObject refreshAccessToken(String refreshToken) {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
            HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+APP_ID+"&grant_type=refresh_token&refresh_token="+refreshToken);
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;
        } catch (Exception error) {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }

    /**
     * 获取成功发布列表
     * @return
     */
    public static JSONObject getFreePublish(String accToken,int startNum) {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        try {
//            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token="+accToken);
            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token="+accToken);
            httpPost.setHeader("content-type", "application/json");

            String body="{"+
                    "\"offset\":"+startNum+","+
                    "\"count\":20"+

                    "}";
            StringEntity reqentity = new StringEntity(body, "utf-8");
            httpPost.setEntity(reqentity);

            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String res = EntityUtils.toString(httpEntity, "UTF-8");
            jsonObject = JSONObject.parseObject(res);
            return jsonObject;
        } catch (Exception error) {
            jsonObject.put("errcode", 405);
            jsonObject.put("errmsg", error.getMessage());
            return jsonObject;
        }
    }


//    public static JSONObject preOrder(WeiXinOrderParam param) {
//        JSONObject jsonObject = new JSONObject();
//        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//        CloseableHttpResponse httpResponse = null;
//        try {
//            HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
//            httpPost.setHeader("content-type", "application/json");
//            StringEntity reqentity = new StringEntity(JSONObject.toJSONString(param), "utf-8");
//            httpPost.setEntity(reqentity);
//            httpResponse = httpClient.execute(httpPost);
//            HttpEntity httpEntity = httpResponse.getEntity();
//            String res = EntityUtils.toString(httpEntity, "UTF-8");
//            jsonObject = JSONObject.parseObject(res);
//            return jsonObject;
//        } catch (Exception error) {
//            jsonObject.put("errcode", 405);
//            jsonObject.put("errmsg", error.getMessage());
//            return jsonObject;
//        }
//    }

//    public static JSONObject refund(Refunds refunds) {
//        JSONObject jsonObject = new JSONObject();
//        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//        CloseableHttpResponse httpResponse = null;
//        try {
//            HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");
//            httpPost.setHeader("content-type", "application/json");
//            StringEntity reqentity = new StringEntity(JSONObject.toJSONString(refunds), "utf-8");
//            httpPost.setEntity(reqentity);
//            httpResponse = httpClient.execute(httpPost);
//            HttpEntity httpEntity = httpResponse.getEntity();
//            String res = EntityUtils.toString(httpEntity, "UTF-8");
//            jsonObject = JSONObject.parseObject(res);
//            return jsonObject;
//        } catch (Exception error) {
//            jsonObject.put("errcode", 405);
//            jsonObject.put("errmsg", error.getMessage());
//            return jsonObject;
//        }
//    }


}
